package com.ejuornal.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.ejuornal.bean.Message;
import com.ejuornal.bean.Page;
import com.ejuornal.service.MessageService;
import com.ejuornal.util.Constants;

public class MessageAction extends BaseAction<Message>{
    private static final long serialVersionUID = -7286700241992542000L;
    private MessageService msgService;
    private Page<Message> pages;
    private Message msg;
    private Map<String, Object> dataMap = new HashMap<String, Object>();
    private String url;
    
    public String hasRead() {
    	int id = super.getRequestParameter();
    	msgService.updateIsRead(id);
    	return SUCCESS;
    }
    
    public String post_list() {
        String sender = super.getUserNameBySession();
        if(sender == null) {
        	return ERROR;
        }
        
        String url="";
        String hsql = "where sender=?";
        pages = msgService.find((page - 1) * 10, url, hsql, new Object[]{sender});
        return SUCCESS;
    }
    
    public String get_list() {
    	String receiver = super.getUserEmailBySession();
        if(receiver == null) {
         	return ERROR;
        }
        String url="";
        String hsql = "where receiver=?";
        pages = msgService.find((page - 1) * 10, url, hsql, new Object[]{receiver});
        
        return SUCCESS;
    }
    
    public String add() {
    	String user_name = (String) super.getSession().get(Constants.USERSESSION);
    	if(user_name == null){
    		return ERROR;
    	}
        msg.setSender(user_name);
        msgService.add(msg);
        return SUCCESS;
    }

    public String isMsgHasNew() {
        String receiver = super.getUserEmailBySession();

        dataMap.clear();
        dataMap.put("yes_1", "0");
        
        if(msgService.findNew(receiver) == true) {
            dataMap.clear();
            dataMap.put("yes_1", "1");
        } 
        
        return SUCCESS;
    }
    
    public String delete() {
        Long id= Long.parseLong(super.getRequest().getParameter("id"));
        String status = super.getRequest().getParameter("status");
        if(id == null && status == null) {
        	return ERROR;
        }
    	if(msgService.delete(id.longValue())) {
    		if(status.equals("get")) {
    			url = "msg_get_list";
    		} else {
    			url = "msg_post_list";
    		}
    		return SUCCESS;
    	}
        return ERROR;
    }
    
   public String detail() {
	   Long id= Long.parseLong(super.getRequest().getParameter("id"));
	   String status = super.getRequest().getParameter("status");
       if(id == null && status == null) {
       	return ERROR;
       }
	   msg = msgService.findById(id);
	   if(status.equals("get")) {
		   url = "/space/msg_detail_get.jsp";
		   return SUCCESS;
	   } else {
		   url = "/space/msg_detail_post.jsp";
		   return SUCCESS;
	   }
	   
   }
    
    public MessageService getMsgService() {
        return msgService;
    }
    @Resource(name="messageService")
    public void setMsgService(MessageService msgService) {
        this.msgService = msgService;
    }
    public Page<Message> getPages() {
        return pages;
    }

    public void setPages(Page<Message> pages) {
        this.pages = pages;
    }

    public Message getMsg() {
        return msg;
    }

    public void setMsg(Message msg) {
        this.msg = msg;
    }
    public Map<String, Object> getDataMap() {
        return dataMap;
    }

    public void setDataMap(Map<String, Object> dataMap) {
        this.dataMap = dataMap;
    }

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
}
